<template>
  <div>
    <!-- 粒子背景 -->
    <vue-particles id="tsparticles" :options="options" />
    <div class="formContainer">
      <h3>企业门户内容管理系统</h3>
      <!-- element表单 -->
      <el-form
        ref="loginFormRef"
        style="max-width: 600px"
        :model="loginForm"
        status-icon
        :rules="loginRules"
        label-width="auto"
        class="loginform"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()"> 登陆 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import axios from "axios";
import { ElMessage } from 'element-plus'
// import store from "@/store";
import { useStore } from "vuex";
const store = useStore();

//登陆element
//表单绑定的响应式对象
const loginForm = reactive({
  username: "",
  password: "",
});

//表单的引用对象 用于校验表单
const loginFormRef = ref();

//校验
const loginRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const router = useRouter();
//提交登陆的函数
const submitForm = () => {
  //1校验表单
  //手动触发.validate为element函数，结果true为输入无误 false为输入不合格
  loginFormRef.value.validate((valid: any) => {
    // console.log('是否正确输入了', valid)
    //2拿到内容提交后台
    if (valid) {
      // console.log("输入正确 获取双向绑定登陆账号值", loginForm);
      //3成功后 设置token
      //axios提交到后端接口express的接口
      axios.post("/adminapi/user/login", loginForm).then(res=>{
        console.log("前端-用户输入账号密码后 提交给后端处理后返回信息", res.data);
        //如果数据库里有你登陆的账号密码(登陆成功)，则跳转到首页 
        //这里的OK(login was successful)一定和后端admin/UserController内对起来
        if(res.data.ActionType === "OK(login was successful)"){
          // console.log("登陆成功，前端获取到后端给的用户信息没 跳转到首页", res.data);
          //登陆成功后把用户信息提交个mutations 存到vuex里
          store.commit("changeUserInfo", res.data.data);
          router.push("/index");
          localStorage.setItem("token", "kerwin");
          //在util的axios.config.js内拦截存储token啦

          //分析思路开始 
          //账号登陆成功后 第二天刷新还能访问 时效太长 有问题不安全 解决思路
          //用jwt的凭证存个过期时间 去后端换是否过期 过期后去登陆 否则继续服务
          //前后端都要去处理 思路
          //前端发的是登陆接口时 后端验证成功后(登陆成功)-返回token(加密后的且含过期时间)
          //前端存储token
          //前端访问其他接口 比如用户中心页面/center接口 需带上token，后端验证token是否有效，
          //有效时返回/center，刷新token(每次刷新后重新计时)
          //无效token 返回401
          //前端 if返回401 跳转登陆页面 ，if返回成功 重新存储token
          //前后端简便方法都要对所有请求发出之前进行拦截，
          //前端用axios拦截器，先把本地token取出来，发送给后端
          //后端拿到token进行验证 验证通过把信息给前端
          //返回错误 前端引导挑战到登陆页面 返回正确放行并存token
          //后端也要路由拦截，统一token验证，刷新token
          //分析思路结束
        }else{
          ElMessage.error('用户名和密码不匹配')
        }
      })
      

    }
  });
};

// <!-- 粒子背景 -->
const options = {
  background: {
    color: {
      value: "#2d3a4b",
    },
  },
  fpsLimit: 120,
  interactivity: {
    events: {
      onClick: {
        enable: true,
        mode: "push",
      },
      onHover: {
        enable: true,
        mode: "repulse",
      },
    },
    modes: {
      bubble: {
        distance: 400,
        duration: 2,
        opacity: 0.8,
        size: 40,
      },
      push: {
        quantity: 4,
      },
      repulse: {
        distance: 200,
        duration: 0.4,
      },
    },
  },
  particles: {
    color: {
      value: "#ffffff",
    },
    links: {
      color: "#ffffff",
      distance: 150,
      enable: true,
      opacity: 0.5,
      width: 1,
    },
    move: {
      direction: "none",
      enable: true,
      outModes: "bounce",
      random: false,
      speed: 6,
      straight: false,
    },
    number: {
      density: {
        enable: true,
      },
      value: 80,
    },
    opacity: {
      value: 0.5,
    },
    shape: {
      type: "circle",
    },
    size: {
      value: { min: 1, max: 5 },
    },
  },
  detectRetina: true,
};
</script>

<style scoped lang="scss">
.formContainer {
  padding: 20px;
  width: 500px;
  height: 300px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba($color: #000, $alpha: 0.5);
  color: white;
  text-align: center;
  h3 {
    font-size: 30px;
    margin-bottom: 20px;
  }
}
:deep() {
  .el-form-item__label {
    color: #fff;
  }
  .el-form-item__content {
    justify-content: center;
  }
}
</style>
